Learning Perl by Randal L. Schwartz

Learning Perl by Randal L. Schwartz

Author:Randal L. Schwartz
Language: eng
Format: epub, pdf
Publisher: O'Reilly Media
Published: 2016-10-12T00:00:00+00:00


The next Operator

Sometimes you’re not ready for the loop to finish, but you’re done with the current iteration. That’s what the next operator is good for. It jumps to the inside of the bottom of the current loop block. After next, control continues with the next iteration of the loop (much like the continue operator in C or a similar language):

# Analyze words in the input file or files while (<>) { foreach (split) { # break $_ into words, assign each to $_ in turn $total++; next if /\W/; # strange words skip the remainder of the loop $valid++; $count{$_}++; # count each separate word ## next comes here ## } } print "total things = $total, valid words = $valid\n"; foreach $word (sort keys %count) { print "$word was seen $count{$word} times.\n"; }

This one is a little more complex than most of our examples up to this point, so let’s take it step by step. The while loop is reading lines of input from the diamond operator, one after another, into $_; you’ve seen that before. Each time through that loop, another line of input will be in $_.

Inside that loop, the foreach loop iterates over the return value split. Do you remember the default for split with no arguments? That splits $_ on whitespace, in effect breaking $_ into a list of words. Since the foreach loop doesn’t mention some other control variable, the control variable will be $_. So, you’ll see one word after another in $_.

But didn’t we just say that $_ holds one line of input after another? Well, in the outer loop, that’s what it is. But inside the foreach loop, it holds one word after another. It’s no problem for Perl to reuse $_ for a new purpose; this happens all the time.

Now, inside the foreach loop, you’re seeing one word at a time in $_. $total is incremented, so it must be the total number of words. But the next line (which is the point of this example) checks to see whether the word has any nonword characters—anything but letters, digits, and underscores. So, if the word is Tom's, or if it is full-sized, or if it has an adjoining comma, quote mark, or any other strange character, it will match that pattern and you’ll skip the rest of the loop, going on to the next word.

But let’s say that it’s an ordinary word, like fred. In that case, you count $valid up by one, and also $count{$_}, keeping a count for each different word. So, when you finish the two loops, you’ve counted every word in every line of input from every file the user wanted you to use.

We’re not going to explain the last few lines. By now, we hope you’ve got stuff like that down already.

Like last, next may be used in any of the five kinds of loop blocks: for, foreach, while, until, or the naked block. Also, if you nest loop blocks, next works with the innermost one.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.